//
//  rightSubTitleVC.m
//  dataGathering
//
//  Created by apple on 14-11-5.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "rightSubTitleVC.h"
#import "mainVC.h"
#import "RKDropdownAlert.h"
#import "WorksetUploadManager.h"

@interface rightSubTitleVC ()

@end

@implementation rightSubTitleVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadButtons];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadButtons{
    MainVCState state = ((mainVC*)(self.parentViewController)).state;
    if(state != MainVCStateNewCollection){
        NSLog(@"need buttons");
        
        _exportButton = [[UIButton alloc] initWithFrame:CGRectMake(392-128-24, 40, 72, 72)];
        [_exportButton setImage:[UIImage imageNamed:@"btn_导出采集数据_u1328.png"] forState:UIControlStateNormal];
        [_exportButton setImage:[UIImage imageNamed:@"btn_导出采集数据_u1328_selected.png"] forState:UIControlStateSelected];
        [_exportButton addTarget:self action:@selector(toExportDataView) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_exportButton];
        
        UIButton *syncButton = [[UIButton alloc] initWithFrame:CGRectMake(392-56-24, 40, 72, 72)];
        [syncButton setImage:[UIImage imageNamed:@"btn_同步_u1326.png"] forState:UIControlStateNormal];
        [syncButton setImage:[UIImage imageNamed:@"btn_同步_u1326_selected.png"] forState:UIControlStateHighlighted];
        [syncButton addTarget:self action:@selector(startSync) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:syncButton];
        
        //if(state == MainVCStateMyCollection){
            _editButton = [[UIButton alloc] initWithFrame:CGRectMake(392-128-72-24, 40, 72, 72)];
            [_editButton setImage:[UIImage imageNamed:@"btn_编辑信息_u1330.png"] forState:UIControlStateNormal];
            [_editButton setImage:[UIImage imageNamed:@"btn_编辑信息_u1330_selected.png"] forState:UIControlStateHighlighted];
            [_editButton setImage:[UIImage imageNamed:@"btn_编辑信息_u1330_selected.png"] forState:UIControlStateSelected];
            [_editButton addTarget:self action:@selector(toEditDataView) forControlEvents:UIControlEventTouchUpInside];
            
            [self.view addSubview:_editButton];
        //}
    }
}

- (void)startSync
{
    [SVProgressHUD showWithStatus:@"同步中,请稍候..." maskType:SVProgressHUDMaskTypeBlack];
    
    //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[WorksetUploadManager sharedInstance]syncSingleWorkset:[[currentWorkSetManager sharedInstance] getCurrentWorkSet] totalSyncNumber:0 currentSyncCount:0];
    //});
    
    
    //[RKDropdownAlert title:@"正在同步..." message:@"同步中，请稍候..." backgroundColor:[UIColor orangeColor] textColor:[UIColor whiteColor]];
}

- (void)reloadButtons
{
    for (UIView *view in [self.view subviews]){
        if([view isMemberOfClass:[UIButton class]]){
            [view removeFromSuperview];
        }
    }
    [self loadButtons];
}

- (void)toAddNewTaskCollectionView
{
    if([_exportButton isSelected]){
        
    }
    UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
    //[[naviVC topViewController] performSegueWithIdentifier:@"addNewTaskCollection" sender:self];
    [naviVC popToRootViewControllerAnimated:NO];
    [[[naviVC viewControllers] objectAtIndex:0] performSegueWithIdentifier:@"addNewTaskCollection" sender:self];
}

- (void)toEditDataView
{
    if([_editButton isSelected]){
        return;
    }
    if([_exportButton isSelected]){
        [_exportButton setSelected:NO];
        UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
        [naviVC popToRootViewControllerAnimated:NO];
    }
    [_editButton setSelected:YES];
    UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
    [[naviVC topViewController] performSegueWithIdentifier:@"toEditDataView" sender:self];
}

- (void)toExportDataView
{
    if([_exportButton isSelected]){
        return;
    }
    if([_editButton isSelected]){
        [_editButton setSelected:NO];
        UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
        [naviVC popToRootViewControllerAnimated:NO];
    }
    [_exportButton setSelected:YES];
    UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
    [[naviVC topViewController] performSegueWithIdentifier:@"toExportDataView" sender:self];
    //[[[naviVC viewControllers] objectAtIndex:0] performSegueWithIdentifier:@"toExportDataView" sender:self];
}

- (void)removeAddNewTaskCollectionView
{
    UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
    [naviVC popToRootViewControllerAnimated:YES];
    
    NSLog(@"rhere are %lu controllers",(unsigned long)[[naviVC viewControllers] count]);
    //UIViewController *vvv =[[naviVC viewControllers] objectAtIndex:0];
    //[vvv dismissViewControllerAnimated:YES completion:nil];
}

- (void)removeExportDataView
{
    [_exportButton setSelected:NO];
    UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
    [naviVC popToRootViewControllerAnimated:YES];
}

- (void)removeEditDataView
{
    [_editButton setSelected:NO];
    UINavigationController *naviVC = (UINavigationController*)[[self childViewControllers] objectAtIndex:0];
    [naviVC popToRootViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
